<?php
/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/5/27
 * Time: 16:08
 */

class zalipay{
    private $url='http://121.43.200.118:8080/Alipay/QRPay';
    private $key='15397085861';
    private $return_url = '';
    private $advice_url = '';
    function __construct()
    {
        global $web_url;
        $this->return_url = 'http://121.40.55.97/pay.php';
        $this->advice_url = 'http://121.40.55.97/respond.php';
    }
    function pay($order_id,$amount){
        $data['tradeNo']=$order_id;
        $data['key']=$this->key;
        $data['amount']=$amount*100;
        $data['notifyUrl']=$this->advice_url;
        $data['qrFlag']=0;
        $data['sign']=$this->sign($data);
        $query='';
        foreach ($data as $k=>$v){
            $query.=$k.'='.$v.'&';
        }
        $result=file_get_contents($this->url.'?'.$query);
        $result=json_decode($result);
        if($result->RspCd=='000000'){
            return ['status'=>0,'message'=>$result->URL];
        }
        return ['status'=>1,'message'=>$result->RspMsg,'RspCd'=>$result->RspCd];
        //echo '<img src="'.$result->URL.'">';
    }
    
    function sign($data){
        return strtoupper(md5($data['tradeNo']."|".$data['key']."|".$data['amount']));
    }
    function Post($url,$curlPost){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }
}